package com.jxlc.common.utils.file;

import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

public class MockMultipartFile implements MultipartFile {

    private final String name;
    private final String originalName;
    private final byte[] bytes;
    private final String contentType;
 
    public MockMultipartFile(File file, String originalName) throws IOException {
        this.name = file.getName();
        this.originalName = originalName;
        this.bytes = Files.readAllBytes(file.toPath());
        this.contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;
    }
 
    public InputStream getInputStream() {
        return new ByteArrayInputStream(bytes);
    }

    public String getName() {
        return name;
    }

    public String getOriginalName() {
        return originalName;
    }

    public String getContentType() {
        return contentType;
    }

    @Override
    @Nullable
    public String getOriginalFilename() {
        return originalName;
    }
 
    @Override
    public boolean isEmpty() {
        return bytes.length == 0;
    }
 
    @Override
    public long getSize() {
        return bytes.length;
    }
 
    @Override
    public byte[] getBytes() {
        return bytes;
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {

    }
}